package com.news.ui.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.news.R;
import com.news.model.bean.Joke;
import com.news.widget.MyImageView;

import java.util.ArrayList;
import java.util.Collection;

import javax.inject.Inject;

/**
 * Created with Android Studio
 * User: ShiLiang
 * Date: 2017/3/2/0002
 * Project: TodayNews
 */

public class JokeAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Joke> mJokes = new ArrayList<>();

    @Inject
    public JokeAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mJokes.size();
    }

    @Override
    public Object getItem(int i) {
        return mJokes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.joke_item, viewGroup, false);
            view.setTag(new ViewHolder(view));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        Joke model = mJokes.get(i);
        holder.mJokeTime.setText(model.getTime());
        holder.mJokeContent.setText(model.getContent());
        String url = model.getUrl();
        Glide.with(mContext).load(url).asBitmap().into(holder.mJokeImage);
        return view;
    }

    private class ViewHolder {
        private final TextView mJokeTime;
        private final TextView mJokeContent;
        private final MyImageView mJokeImage;

        ViewHolder(View itemView) {
            mJokeTime = (TextView) itemView.findViewById(R.id.joke_time);
            mJokeContent = (TextView) itemView.findViewById(R.id.joke_content);
            mJokeImage = ((MyImageView) itemView.findViewById(R.id.joke_image));
        }
    }

    public void add(Collection<? extends Joke> jokes) {
        this.mJokes.addAll(jokes);
        notifyDataSetChanged();
    }
}
